var hours = new Array();
var dateFormat = 'dd-mm-yy';
$(function(){
	init_hashchange();
	//init_hours();
	init_inputs();
	init_forms();
	$.datepicker.setDefaults(
			{	dateFormat: dateFormat,
				dayNames:['Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado'],
				dayNamesMin: ['Do','Lu','Ma','Mi','Ju','Vi','Sa'],
				dayNamesShort:['Dom','Lun','Mar','Mie','Jue','Vie','Sab'],
				monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
				monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
				minDate: new Date()
			});
	
	if($.validator != undefined){
		$.validator.setDefaults({
				onfocusout: false,
				onkeyup: false,
				onclick: false,
				ignoreTitle:true,
				highlight:function(element, errorClass){
					
				}
		,
				showErrors: function(errorMap, errorList) {
					$('[id*="-error"]').remove();
					for(var i=0;i<errorList.length;i++){
						var item = errorList[i];
						var error = $('<span>');
						$('[id="'+item.element.name + '-error"]').remove();
						var wrapper = $('<div>');
						wrapper.attr('id',item.element.name + '-error');
						error.addClass('error');
						error.text(item.message);
						wrapper.append(error);
						$(item.element).parent().append(wrapper);
					}
				}
		});
		$.validator.messages = {
				required: SCRIPT_MESSAGES['REQUIRED'],
				remote: "Please fix this field.",
				email: SCRIPT_MESSAGES['INVALID_MAIL_FORMAT'],
				url: "Please enter a valid URL.",
				date: "Please enter a valid date.",
				dateISO: "Please enter a valid date (ISO).",
				number: "Please enter a valid number.",
				digits: "Please enter only digits.",
				creditcard: "Please enter a valid credit card number.",
				equalTo: "Please enter the same value again.",
				accept: "Please enter a value with a valid extension.",
				maxlength: $.validator.format("Ha superado la longitud de {0} caracteres."),
				minlength: $.validator.format("Please enter at least {0} characters."),
				rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
				range: $.validator.format("Please enter a value between {0} and {1}."),
				max: $.validator.format("Please enter a value less than or equal to {0}."),
				min: $.validator.format("Please enter a value greater than or equal to {0}.")
			}
	}
	
})
 
function init_hours(){
	var hour_cmp;
	var min_cmp;
	var time_frame = 5;
	for(var i=0;i<=23;i++){
		hour_cmp = i < 10? '0'+i : i;
		for(j=0;j<60/time_frame;j++){
			min_cmp = 60- (60 - (time_frame * j ));
			min_cmp = min_cmp >= 10? min_cmp : '0'+min_cmp;  
			hours.push(hour_cmp+':'+min_cmp);
		}
	}
	
	$('.modalpop').click(
			function(e){
				e.stopPropagation(); 
				e.preventDefault();

				$.ajax({url:$(this).attr('href'),success: function(response)
													{ 
														var popup = $('<div>');
														$('body').append(popup);
														popup.append(response);
														popup.modalpop({msg:true});
													},dataType:"html",type: "get"}); 
				return true;
			});
}

function init_inputs(context){
	context = context == undefined ? 'body':context
	$('input[title!=""]',context).input_example();
	$('textarea[title!=""]',context).input_example();
	$('input[patron]',context).input_prevent();
	$('.calendar-widget',context).datepicker();
}

function init_forms(){
	$("form").submit(function() {
		$('input[title!=""]').each(function() {
			if($(this).realVal()!=undefined && $(this).realVal()!='' && $(this).realVal() == this.title) {
				$(this).val(undefined);
			}
		});
		$('textarea[title!=""]').each(function() {
			if($(this).realVal()!=undefined && $(this).realVal()!='' && $(this).realVal() == this.title) {
				$(this).val(undefined);
			}
		});
	});
}

function loadjscssfile(filename, filetype){
	 if (filetype=="js"){ //if filename is a external JavaScript file
	  var fileref=document.createElement('script')
	  fileref.setAttribute("type","text/javascript")
	  fileref.setAttribute("src", filename)
	 }
	 else if (filetype=="css"){ //if filename is an external CSS file
	  var fileref=document.createElement("link")
	  fileref.setAttribute("rel", "stylesheet")
	  fileref.setAttribute("type", "text/css")
	  fileref.setAttribute("href", filename)
	 }
	 if (typeof fileref!="undefined")
	  document.getElementsByTagName("head")[0].appendChild(fileref)
}

function init_hashchange(){
	$(window).bind( 'hashchange', function(e) {
		var data = $.bbq.getState();
		for (var key in data) {
			 if(data.hasOwnProperty(key)){
				 $(window).trigger(key+'change');
			 }
		}
		 
	});
	
	init_widget_links();
}

function init_widget_links(context){
	context = context == undefined? 'body':context;
	$('a[href^="!#"]',context).bind('click', function(){ 
		var href = $(this).attr('href').replace('!#','');
		var state = $.deparam(href);
		$.bbq.pushState(state);
		//not propagate
		return false;
	});
}
